Layer_Props = Class.create({
    is_disabled_ : false,
    params_ : '',
    initialize: function() 
    {
        $('layerpropsform').observe('submit', this.updateParams.bindAsEventListener(this));
        //document.observe('click', this.checkForUnsavedData.bindAsEventListener(this));
        this.params_ = this.readParams();
    },
    readParams : function()
    {
        var params = 'ajax=true&';
        $$('#layerpropsform select').each(function(element){
            params +=  element.readAttribute('name') + '=' + element.value + '&';
        }.bind(params));
        $$('#layerpropsform input').each(function(element){
            if(element.readAttribute('type') == 'radio' || element.readAttribute('type') == 'checkbox')
            { 
                if(element.checked)
                    params +=  element.readAttribute('name') + '=' + element.value.gsub("#", "") + '&';
            }
            else
                params +=  element.readAttribute('name') + '=' + element.value.gsub("#", "") + '&';
        }.bind(params));
        return params;
    },
    updateParams : function(event)
    {
        params = this.readParams();
        this.params_ = params;
        event.stop();
        if(!this.is_disabled_)
        {
            new Ajax.Request($('layerpropsform').action, {
                method: "post",
                parameters : params,
                onSuccess: this.dataUpdated.bind(this),
                onFailure: function(){$('layerpropsform').submit();},
                onLoading: this.saveData.bind(this)
            });
        }
    },
    saveData : function()
    {
        $('loading_image').show();
        $('layerpropsformsubmit').writeAttribute('disabled', 'disabled');
        this.is_disabled_ = true;
    },
    dataUpdated : function(response)
    {
        if(this.is_disabled_)
            $('layerpropsformsubmit').writeAttribute('disabled', false);
        this.is_disabled_ = false;
        $('loading_image').hide();
        arr_response = response.responseJSON;
        if(arr_response !== null)
        {
            $('ajax_response_message').update(arr_response['message']).show();
            Element.fade.delay(2.5, 'ajax_response_message', { duration: 1.0});
        }
    },
    checkForUnsavedData : function(event)
    {
        var element = Event.findElement(event);
        //console.log(element.tagName);
        if(element.tagName != 'A')
        {
            event.stop();
            return 0;
        }
        params = this.readParams();
        if(this.params_ !== params)
        {
            event.stop();
            this.addOverlay(element);
            //alert("You have unsaved changes.");
        }    
    },
    addOverlay : function(element)
    {
        $$('body').first().insert('<div id="analysis-window-overlay23" style="height:'+document.viewport.getHeight()+'px; width:'+document.viewport.getWidth()+'px;"></div><div style="position:fixed; top: 200px; left: 500px; background: #fff; color: #000;"><p></p><p><a href="">Änderungen speichern</a> <a href="'+element.href+'">Änderungen nicht speichern</a></p></div>');
    }
});
new Layer_Props();
